package com.will.txj.wy.global.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author: wen-yi;
 * @date: 2021/11/21 20:03;
 * @Description:
 */
@Component
@EnableSwagger2
@EnableKnife4j
public class Knife4jConfig {

    @Value("${swagger.host}")
    private String swaggerHost;

    @Value("${swagger.enable}")
    private boolean swaggerEnable;
    @Autowired
    private ApiInfo apiInfo;

    /**
     * 注入docket（swagger-ui）
     * @return
     */
    @Bean("docketServer")
    public Docket docketServer() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 指定路径当前机器ip，动静分离，跨域需要设置
                .host(swaggerHost)
                .enable(swaggerEnable)
                .apiInfo(apiInfo)
                // 分组名称
                .groupName("wy-server").select()
                // 这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.will.txj.wy.server.controller"))
                .paths(PathSelectors.any()).build();
    }

    @Bean("docketFile")
    public Docket docketFile() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 指定路径当前机器ip，动静分离，跨域需要设置
                .host(swaggerHost)
                .enable(swaggerEnable)
                .apiInfo(apiInfo)
                // 分组名称
                .groupName("wy-files").select()
                // 这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.will.txj.wy.files.controller"))
                .paths(PathSelectors.any()).build();
    }

    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("WY API").description("this is wy server")
                .termsOfServiceUrl("http://wenyiyun.love/").contact(new Contact("wy", "http://wenyiyun.love/", "319067125@qq.com"))
                .version("1.0-SNAPSHOT").build();
    }
}
